// pages/recruit/detail/detail.js
var Base64 = require('../../../utils/base64.js');
var upng = require('../../../utils/UPNG.js');
const time = require('../../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recruit_info: [],
    flag_group: [],
    recruit_phone: '',
    job_description: '',
    company_description: '',
    deatil_id: '',
    recruit_id: '',

    pay_low: '',
    pay_high: '',
    keep_pay: '',
    plat_name: '',
    welfare: '',

    flag: true,
    flag_time: true,
    flag_share: true,
    text_time: '',
    reg_true: false,

    flag_release: true,
    flag_id: true,

    role: '',

    //more_show
    more_show: true,
    flag_phone: true,
    shape_show: true,
    flag_out: true,

    flag_info: true,
    flag_weixin: false,
    flag_qq: false,
    flag_email: false,


    //简历信息获取
    phone_value: '',
    is_resume: '',
    nickname: '',
    age: '',
    sex: '',


  },
  //模态框 拨打
  click_phone: function() {
    //管理员、会长
    wx.makePhoneCall({
      phoneNumber: '0571-87081736',
    })
    this.setData({
      flag_contact: true,
    })
  },
  //模态框 复制
  click_modal_copy: function() {
    wx.setClipboardData({
      data: 'zhibozhijia888',
      success: function(e) {}
    })
  },
  //发送简历
  click_send() {
    let that = this;
    if (this.data.is_resume == 0) {
      //去填写简历
      this.setData({
        flag_out: false,
      })
      // wx.showModal({
      //   content: '制作简历，更容易c位出道哦!',
      //   showCancel: true,
      //   confirmText: 'C位出道',
      //   confirmColor: '#d0a76c',
      //   success: function(e) {
      //     if (e.confirm) {
      //       wx.navigateTo({
      //         url: '../../resume/uploadResume/uploadResume',
      //       })
      //     }
      //   },
      // })

    } else {
      //去发送
      wx.request({
        url: app.globalData.url + 'api/resume/send/succeed',
        data: {
          token: app.globalData.token,
          type: 2,
          recruit_id: that.data.deatil_id
        },
        method: 'POST',
        success: (e) => {
          console.log(e)
          let chat_id = e.data.data.chat_id;
          this.getcount();
          if (this.data.count > 0) {
            // this.session(resume_id);
            wx.navigateTo({
              url: '../session/index?chat_id=' + chat_id
            })
            // this.link_up_anchor(0, resume_id);
          } else {
            wx.showToast({
              title: '发送成功!',
              icon: 'none',
            })
          }
          that.setData({
            more_show: true,
          })
        },
      })
    }
  },
  getcount() {
    wx.request({
      url: app.globalData.url + 'api/swop/guild/number',
      data: {
        token: app.globalData.token
      },
      method: 'POST',
      success: (e) => {
        let count = e.data.data.count;
        console.log('count=' + count)
        this.setData({
          count: count,
        })
      },
    })
  },
  //点击约聊
  click_session(e) {
    //   console.log(e.currentTarget.dataset.resume_id);
    let resume_id = e.currentTarget.dataset.resume_id;
    wx.request({
      url: app.globalData.url + 'api/guild/chat/anchor',
      data: {
        token: app.globalData.token,
        resume_id: resume_id,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        var chat_id = e.data.data.chat_id;
        this.getcount();
        if (this.data.count > 0) {
          // this.session(resume_id);
          wx.navigateTo({
            url: '../session/index?chat_id=' + chat_id
          })
          // this.link_up_anchor(0, resume_id);
        } else {
          wx.showToast({
            title: '次数不足!',
            icon: 'none',
          })
        }
      }
    })
  },
  //go_company
  go_company(e) {
    // console.log(e.currentTarget.dataset.home_url)
    let company_name = e.currentTarget.dataset.company_name,
      token = e.currentTarget.dataset.token;
    if (company_name) {
      wx.navigateTo({
        url: '../../mine/anchorWelfare/checkDetal/checkDetal?token=' + token,
      })
      // wx.navigateTo({
      //   url: '../../protocol/protocol?url=' + url + '&title=公司详情',
      // })
    } else {
      wx.showToast({
        title: '该公司暂未完善主页信息',
        icon: 'none',
      })
    }

  },

  // QQ/微信/邮箱
  click_info: function(e) {
    var that = this;
    // console.log(e.currentTarget.dataset.type)
    if (this.data.is_resume == 0) {
      //去填写简历、
      this.setData({
        flag_out: false,
      })
      // wx.showModal({
      //   content: '制作简历，更容易c位出道哦!',
      //   showCancel: true,
      //   confirmText: 'C位出道',
      //   confirmColor: '#d0a76c',
      //   success: function(e) {
      //     if (e.confirm) {
      //       wx.navigateTo({
      //         url: '../../resume/uploadResume/uploadResume',
      //       })
      //     }
      //   },
      // })
    } else {
      this.setData({
        flag_info: false,
        show_modalWrap: true,
        flag_qq: false,
        flag_weixin: false,
        flag_email: false,
        more_show: true,
      })
      var _type = e.currentTarget.dataset.type;
      if (_type == "qq") {
        wx.setClipboardData({
          data: that.data.recruit_info.qq
        })
        this.setData({
          flag_qq: true,
        })
      } else if (_type == "weixin") {
        wx.setClipboardData({
          data: that.data.recruit_info.weixin
        })
        this.setData({
          flag_weixin: true,
        })
      } else if (_type == "email") {
        wx.setClipboardData({
          data: that.data.recruit_info.email
        })
        this.setData({
          flag_email: true,
        })
      } else {}
    }

  },

  //clear_num
  clear_num: function(e) {
    this.setData({
      phone_value: '',
    })
  },
  // 出道/确定
  click_out_sure: function(e) {
    this.setData({
      flag_out: true,
      more_show: true,
    })
    wx.navigateTo({
      url: '../../resume/uploadResume/uploadResume?detail=1',
    })
  },
  //去选择身份
  click_identity() {
    wx.navigateTo({
      url: '../../selectIdentity/selectIdentity',
    })
  },


  //打电话确认
  call_sure: function() {
    let that = this;
    // console.log('判断+' + that.data.reg_true)
    if (that.data.reg_true == true) {
      wx.request({
        url: app.globalData.url + 'api/resume/send/succeed',
        method: 'POST',
        data: {
          token: app.globalData.token,
          type: 1,
          recruit_id: that.data.recruit_id,
          telephone: that.data.phone_value,
        },
        success: function(e) {
          // console.log(e)
          that.setData({
            flag_phone: true,
          })
          wx.showToast({
            title: '操作成功~',
            icon: "none",
          })
        }
      })
    } else {
      console.log('失败')
      wx.showToast({
        title: '手机号格式错误~',
        icon: "none",
      })
    }

  },
  //input phone
  input_phone: function(e) {
    let that = this;
    // console.log(e.detail.value)

    let str = e.detail.value;
    let reg = /^1[3|4|5|7|8|9][0-9]{9}$/;
    let flag = reg.test(str);
    // console.log(flag)
    if (flag) {
      that.setData({
        reg_true: true,
        phone_value: e.detail.value,
      })
    }

  },
  //点击 留电话
  click_stay: function() {
    let that = this;
    if (this.data.is_resume == 0) {
      //去填写简历
      this.setData({
        flag_out: false,
      })
      // wx.showModal({
      //   content: '制作简历，更容易c位出道哦!',
      //   showCancel: true,
      //   confirmText: 'C位出道',
      //   confirmColor: '#d0a76c',
      //   success: function(e) {
      //     if (e.confirm) {
      //       wx.navigateTo({
      //         url: '../../resume/uploadResume/uploadResume',
      //       })
      //     }
      //   },
      // })
    } else {
      that.setData({
        more_show: true,
        flag_phone: false,
      })
    }

  },
  //点击 打电话
  click_tel: function() {
    if (this.data.is_resume == 0) {
      //去填写简历
      this.setData({
        flag_out: false,
      })
      // wx.showModal({
      //   content: '制作简历，更容易c位出道哦!',
      //   showCancel: true,
      //   confirmText: 'C位出道',
      //   confirmColor: '#d0a76c',
      //   success: function(e) {
      //     if (e.confirm) {
      //       wx.navigateTo({
      //         url: '../../resume/uploadResume/uploadResume',
      //       })
      //     }
      //   },
      // })

    } else {
      this.setData({
        more_show: true,
      })
      this.apply();
      this.anchor_call();
    }

  },
  //关闭
  click_close: function() {
    this.setData({
      more_show: true,
    })
  },
  //模块框 Start
  modal_hide: function() {
    this.setData({
      flag: true,
      flag_time: true,
      flag_share: true,
      flag_phone: true,
      shape_show: true,
      flag_out: true,
      flag_contact: true,
      flag_info: true,
      more_show: true,

      flag_release: true,
      flag_id: true,
    })
  },
  //模块框 End

  //转发
  go_share: function(e) {
    let that = this;
    if (wx.getStorageSync('reg_register_role') == 0) {
      this.setData({
        flag_id: false
      })
    } else {
      if (this.data.flag == false) {
        this.setData({
          flag: true,
          flag_time: false,
        })
      }
      //分享后的次数
      wx.request({
        url: app.globalData.url + 'api/recruit/anchor/surplus/make/call',
        method: 'POST',
        data: {
          token: app.globalData.token,
          recruit_id: that.data.recruit_id
        },
        success: function(e) {
          // console.log(e)
          if (e.data.data.is_share == "1") {
            console.log("分享过了")
            that.setData({
              flag_share: false,
              flag_time: true,
            })
          } else {
            //主播分享>>增加次数
            wx.request({
              url: app.globalData.url + 'api/recruit/anchor/share',
              method: 'POST',
              data: {
                token: app.globalData.token,
                recruit_id: that.data.recruit_id
              },
              success: function(e) {
                // console.log(e)
                wx.request({
                  url: app.globalData.url + 'api/recruit/anchor/surplus/make/call',
                  method: 'POST',
                  data: {
                    token: app.globalData.token,
                    recruit_id: that.data.recruit_id
                  },
                  success: function(e) {
                    //播报次数
                    that.setData({
                      text_time: e.data.data.number,
                      flag_time: false,
                    })
                  }
                })

              }
            })
          }
        }
      })
    }
  },
  //recruit列表
  recruit_list: function(deatil_id) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/read/recruit/xcx/detail',
      method: "POST",
      data: {
        token: app.globalData.token,
        recruit_id: deatil_id,
      },
      success: function(e) {
        console.log(e)
        if (e.data.code == "-2") {
          wx.redirectTo({
            url: '../../author/author',
          })
        }
        if (!e.data.data.xcx[0].job_description) {} else {
          let strs1 = (e.data.data.xcx[0].job_description).toString();
          // console.log(strs1)

          let strs_br = strs1.replace(/<br\/>/g, "\n");

          let strs11 = strs_br.replace(/<\/?.+?>/g, "");
          let job_description = strs11.replace(/&nbsp;/g, ""); //str为得到后的内容
          that.setData({
            job_description: job_description,
          })
        }

        if (!e.data.data.xcx[0].company_description) {
          // console.log(e)
        } else {
          let strs2 = (e.data.data.xcx[0].company_description).toString();

          let strs_br = strs2.replace(/<br\/>/g, "\n");

          let strs22 = strs_br.replace(/<\/?.+?>/g, "");
          let company_description = strs22.replace(/&nbsp;/g, ""); //str为得到后的内容
          that.setData({
            company_description: company_description,
          })
        }


        // console.log(e.data.data.xcx[0].pay_low)
        //pay_low
        if (e.data.data.xcx[0].pay_low > 1000) {
          var low_str = (e.data.data.xcx[0].pay_low).toString();
          var pay_low = low_str.substring(0, low_str.length - 3) + "k";
        } else {
          var pay_low = (e.data.data.xcx[0].pay_low).toString();
        }

        //pay_high
        if (e.data.data.xcx[0].pay_low > 1000) {
          var high_str = (e.data.data.xcx[0].pay_high).toString();
          var pay_high = high_str.substring(0, high_str.length - 3) + "k";
        } else {
          var pay_high = (e.data.data.xcx[0].pay_high).toString();
        }

        //keep_pay
        if (e.data.data.xcx[0].keep_pay > 1000) {
          var keep_str = (e.data.data.xcx[0].keep_pay).toString();
          var keep_pay = keep_str.substring(0, keep_str.length - 3) + "k";
        } else {
          var keep_pay = (e.data.data.xcx[0].keep_pay).toString();
        }

        //平台
        // console.log(e.data.data.xcx[0].plat_name)
        if (e.data.data.xcx[0].plat_name) {
          let plat_name = e.data.data.xcx[0].plat_name.split("|").join(" | ");
          that.setData({
            plat_name: plat_name,
          })
        } else {}

        //线上线下
        // console.log(e.data.data.xcx[0].job_method)
        if (e.data.data.xcx[0].job_method) {
          //  console.log(e.data.data.xcx[0].welfare)
          let method_num = e.data.data.xcx[0].job_method;

          switch (method_num) {
            case 1:
              var job_method = "线上";
              break;
            case 2:
              var job_method = "线下";
              break;
            case 3:
              var job_method = "线上/线下";
              break;
            default:
              var job_method = "";
          }
          let flag_group = that.data.flag_group;
          flag_group.push(job_method)
          that.setData({
            flag_group: flag_group,
          })
          // console.log(that.data.flag_group)
        } else {}

        //结算方式
        // console.log(e.data.data.xcx[0].salary_type)
        if (e.data.data.xcx[0].salary_type) {
          console.log("走这里")
          //  console.log(e.data.data.xcx[0].welfare)
          let salary_num = e.data.data.xcx[0].salary_type;

          switch (salary_num) {
            case 1:
              var salary_type = "月结";
              break;
            case 2:
              var salary_type = "周结";
              break;
            case 3:
              var salary_type = "日结";
              break;
            case 4:
              var salary_type = "日结/周结/月结";
              break;
            default:
              var salary_type = "";
          }
          let flag_group = that.data.flag_group;
          flag_group.push(salary_type)
          console.log("flag_group+" + flag_group)
          that.setData({
            flag_group: flag_group,
          })
          // console.log(that.data.flag_group)
        } else {}

        //福利
        // console.log(e.data.data.xcx[0].welfare)
        if (e.data.data.xcx[0].welfare) {
          //  console.log(e.data.data.xcx[0].welfare)
          let welfare_regBef = e.data.data.xcx[0].welfare;
          let welfare_regAft = welfare_regBef.replace(/，|,|、/g, ",");
          let welfare = welfare_regAft.split(",");

          let flag_group = that.data.flag_group;
          flag_group.push(...welfare)
          that.setData({
            flag_group: flag_group,
          })
        } else {}

        //发布时间
        var recruit_info = e.data.data.xcx[0];
        recruit_info.publish_time = time.formatTimeTwo(recruit_info.publish_time, 'Y.M.D');

        // console.log(job_description)
        that.setData({
          recruit_info: recruit_info,
          recruit_phone: e.data.data.xcx[0].contact_phone,
          recruit_id: e.data.data.xcx["0"].id,

          pay_low: pay_low,
          pay_high: pay_high,
          keep_pay: keep_pay,
        })
      }
    })
  },
  //收藏
  go_collection: function(e) {
    // console.log(e.currentTarget.dataset.recruit_id)
    if (wx.getStorageSync('reg_register_role') == 0) {
      this.setData({
        flag_id: false
      })
    } else {
      wx.showLoading({
        title: '操作中~',
      })
      let that = this;
      if (this.data.recruit_info.is_collect == "1") {
        //取消收藏
        wx.request({
          url: app.globalData.url + 'api/recruit/cancel/collect',
          method: 'POST',
          data: {
            token: app.globalData.token,
            recruit_id: e.currentTarget.dataset.recruit_id,
          },
          success: function(e) {
            // console.log(e);
            that.recruit_list(that.data.deatil_id)
            wx.hideLoading();
          }
        })
      } else {
        //未收藏
        wx.request({
          url: app.globalData.url + 'api/recruit/do/collect',
          method: 'POST',
          data: {
            token: app.globalData.token,
            recruit_id: e.currentTarget.dataset.recruit_id,
          },
          success: function(e) {
            // console.log(e)
            if (e.data.code == "-1") {
              wx.hideLoading();
              wx.showToast({
                icon: 'none',
                title: e.data.message,
              })
            } else {
              wx.hideLoading();
              wx.showToast({
                icon: 'none',
                title: '收藏成功',
              })
            }
            that.recruit_list(that.data.deatil_id)
          }
        })
      }
    }


  },
  //举报
  go_accusation: function() {
    wx.showToast({
      title: '举报成功',
    })
  },
  //安全提示
  go_protocol: function() {
    wx.navigateTo({
      url: '../../protocol/protocol?src=api/sign/recruit/page&&title=安全提示',
    })
  },
  //主播拨打电话
  anchor_call() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/recruit/anchor/make/telephone',
      data: {
        token: app.globalData.token,
        recruit_id: this.data.deatil_id,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
      },
    })
  },
  //主播求职申请 以及剩余次数
  apply: function(e) {
    let that = this;
    //主播
    wx.request({
      url: app.globalData.url + 'api/recruit/anchor/make/call',
      method: 'POST',
      data: {
        token: app.globalData.token,
        recruit_id: this.data.deatil_id,
      },
      success: function(e) {
        console.log(e)
        if (e.data.data.is_call == "1") {
          //is_call=1,直接打电话
          if (that.data.recruit_phone) {
            wx.makePhoneCall({
              phoneNumber: that.data.recruit_phone,
            })

          } else {
            wx.showToast({
              icon: "none",
              title: '联系方式更新中~',
            })
          }
        } else {
          //走原先流程
          if (e.data.data.number <= '0') {
            console.log('您的次数已用完，请分享页面增加次数')
            that.setData({
              flag: false,
            })
          } else {
            //打电话
            if (that.data.recruit_phone) {
              wx.makePhoneCall({
                phoneNumber: that.data.recruit_phone,
              })
            } else {
              wx.showToast({
                icon: "none",
                title: '联系方式更新中~',
              })
            }
          }
        }

      }
    })
  },
  //简历状态
  resumeStatus() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/resume/status',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        let status = e.data.data.status;
        that.setData({
          status: status
        })
      },
    })
  },
  //打电话
  click_call: function() {
    let that = this;
    if (wx.getStorageSync('reg_register_role') == 0) {
      this.setData({
        flag_id: false
      })
    } else {
      //发送申请
      if (app.register_info.register_role == "2") {
        let that = this;
        var status = this.data.status;
        // if (that.data.is_resume == '0') {
        if (status == '-1') {
          this.setData({
            flag_out: false,
          })
          // wx.showModal({
          //   content: '制作简历，更容易c位出道哦!',
          //   showCancel: true,
          //   confirmText: 'C位出道',
          //   confirmColor: '#d0a76c',
          //   success: function(e) {
          //     if (e.confirm) {
          //       wx.navigateTo({
          //         url: '../../resume/uploadResume/uploadResume',
          //       })
          //     }
          //   },
          // })
          // wx.navigateTo({
          //   url: '../../resume/uploadResume/uploadResume',
          // })
          // wx.showToast({
          //   title: '请先上传简历',
          //   icon: 'none',
          // })
        } else if (status == '0') {
          wx.showToast({
            title: '简历审核中，暂不能操作',
            icon: 'none',
          })
        } else if (status == '1') {
          that.setData({
            more_show: false,
          })
          wx.request({
            url: app.globalData.url + 'api/recruit/list',
            method: 'POST',
            data: {
              token: app.globalData.token,
            },
            success: function (e) {
              console.log('有简历+' + e.data.data.is_resume)
              that.setData({
                is_resume: e.data.data.is_resume,
              })
            }
          })
          return false;
        } else if (status == '2') {
          wx.showToast({
            title: '简历审核失败，请重新上传',
            icon: 'none',
          })
        }
      } else {
        //管理员、会长
        // wx.makePhoneCall({
        //   phoneNumber: '0571-87081736',
        // })
        this.setData({
          flag_release: false,
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // if (!options.deatil_id) {
    //   wx.switchTab({
    //     url: '../index/index',
    //   })
    // }
    let that = this;
    this.getcount();
    this.setData({
      deatil_id: options.deatil_id,
      role: app.register_info.register_role,
    })
    this.recruit_list(this.data.deatil_id)

    if (this.data.role == 2) {
      //加载简历数据
      wx.request({
        url: app.globalData.url + 'api/resume/index',
        method: 'POST',
        data: {
          token: app.globalData.token,
        },
        success: function(e) {
          console.log(e)

          if (e.data.data.resume.telephone != '') {
            that.setData({
              reg_true: true,
            })
          }
          that.setData({
            phone_value: e.data.data.resume.telephone,
            nickname: e.data.data.resume.nickname,
            age: e.data.data.resume.age,
            sex: e.data.data.resume.sex,
          })
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.resumeStatus();
    if (!this.data.deatil_id) {
      wx.reLaunch({
        url: '../index/index',
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '推荐职位',
      path: '/page/recruit/detail/detail'
    }
  },
})